<?php
class OrdencomprasController extends AppController {

	var $name = 'Ordencompras';
	var $components = array('RequestHandler');
	
	function index(){
		$this->layout="ajax";		
		if($this->JQGridPaginate->isGrid){
			$fields = array('Ordencompra.id',
							'Ordencompra.numero',
							'"Proveedor"."razonsocial" AS "Ordencompra__proveedor"',
							'"Moneda"."nombre" AS "Ordencompra__moneda"',
							'Ordencompra.prc_total_sigv',
							'Ordencompra.prc_total_cigv',
							'"Ordencompra"."created"::date AS "Ordencompra__created"',
			                '"Ordencompraestado"."descripcion" AS "Ordencompra__estado"');
			
			$this->JQGridPaginate->recursive = 0;
			$this->JQGridPaginate->cakefields = $fields;
			$this->JQGridPaginate->condition = array('Ordencompra.estado_id'=>1);
			$this->autoRender = false;
			echo $this->JQGridPaginate->paginate("Ordencompra");
			
		}elseif ($this->RequestHandler->isPost()){			
			$this->autoRender = false;
			$oper = $this->params['form']['oper'];
			if($oper == "add"){
				$this->Ordencompra->create();				 
			}elseif ($oper == "edit"){
				$this->data['Ordencompra']['id'] = $this->params['form']['id'];
			}elseif ($oper == "del"){
				$this->data['Ordencompra']['id'] = $this->params['form']['id'];
				$this->data['Ordencompra']['estado_id'] = 2;				
			}
			
			if(!$this->Ordencompra->save($this->data)){
				echo '';
			}else{
				echo true;
			}	
		}		
	}
	
	function add($id = null){
		$this->layout = 'ajax';
		if(!empty($id) && !($id === 'undefined')){
			$this->data = $this->Ordencompra->getOrdencompra($id);
		}
		
		$this->loadModel('Proveedor');
		$listaProveedores = $this->Proveedor->find('list',array('fields'=>array('Proveedor.id','Proveedor.razonsocial'),
														'order'=>'Proveedor.razonsocial'));
		$this->loadModel('Moneda');
		$listaMonedas = $this->Moneda->find('list',array('fields'=>array('Moneda.id','Moneda.nombre'),
												'order'=>'Moneda.nombre','conditions'=>array('Moneda.id'=>array(1,2))));
		$this->set('monedas', $listaMonedas);
		$this->set('proveedors', $listaProveedores);
	}
	
	function actualizar(){ 
	    Configure::write('debug', 0);
	    $accion = empty($this->params['named']['oper'])?'eliminar':$this->params['named']['oper'];
		$this->layout = 'ajax';
    	
		if($accion == 'update'){ 
			$this->Ordencompra->begin();
			/** editamos o agregamos **/
			$respuesta = $this->Ordencompra->setGuardarOrdencompra($this->data,$this->variables);
			if($respuesta['respuesta']){
				$this->Ordencompra->commit();
				echo json_encode($respuesta);
			}else{
				$this->Ordencompra->rollback();
				echo json_encode($respuesta);
			}
		}else{
			/** Desactivamos la orden de compra **/
			if(!empty($this->data['Ordencompra']['id'])){
				$ordencompra['Ordencompra']['id'] = $this->data['Ordencompra']['id'];
				$ordencompra['Ordencompra']['estado_id'] = 2;
				@$this->Ordencompra->save($ordencompra['Ordencompra']);
				echo json_encode($this->Ordencompra->visualizarError($this->Ordencompra->validationErrors));
			}
		}
		$this->autoRender = false;
	}	
}